package com.lora.communication.mqtt;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * mqtt回调方法类
 */
public class LoraMqttCallback implements MqttCallback {
    private Logger log = LoggerFactory.getLogger(getClass());

    private LoraMqttHandler loraMqttHandler=new LoraMqttHandler();

    @Override
    public void connectionLost(Throwable throwable) {
        log.error("链接已断开");
        throwable.printStackTrace();
    }

    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        loraMqttHandler.dealWith(topic,mqttMessage);
    }

    /**
     *
     * 功能描述:
     * 消息发送成功后的方法
     * @param:
     * @return:
     * @auther: lwt
     * @date: 2019/9/8
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        String[] topics = iMqttDeliveryToken.getTopics();
        for (String topic : topics) {
            if(iMqttDeliveryToken.isComplete()){
                log.info(topic+"主题消息发送成功!");
            }else{
                log.error(topic+"消息发送失败！");
            }
        }
    }
}
